package com.youhp.spire.xls.demo.Worksheets;

import com.spire.xls.*;

import java.util.EnumSet;

public class ProtectCell {
    public static void main(String[] args) {
        String inputFile = "data/ProtectCell.xlsx";
        String outputFile = "output/ProtectCell_out.xlsx";

        //Create a workbook and load a file
        Workbook workbook = new Workbook();
        workbook.loadFromFile(inputFile);

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Protect cell
        sheet.getCellRange("B3").getCellStyle().setLocked(true);
        sheet.getCellRange("C3").getCellStyle().setLocked(false);

        //Protect sheet
        sheet.protect("TestPassword", EnumSet.of(SheetProtectionType.All));

        //Save the Excel file
        workbook.saveToFile(outputFile, ExcelVersion.Version2013);
    }
}
